/*     */ package com.zimbra.cs.mailbox;
/*     */ 
/*     */ import com.google.common.base.Strings;
/*     */ import com.google.common.collect.Lists;
/*     */ import com.google.common.collect.Maps;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.XMLElement;
/*     */ import com.zimbra.common.soap.XmlParseException;
/*     */ import com.zimbra.common.util.DateUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Cos;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.soap.JaxbUtil;
/*     */ import com.zimbra.soap.mail.type.Policy;
/*     */ import com.zimbra.soap.mail.type.Policy.Type;
/*     */ import com.zimbra.soap.mail.type.RetentionPolicy;
/*     */ import java.util.Collections;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.UUID;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class RetentionPolicyManager
/*     */ {
/*  45 */   private static String FN_KEEP = "keep";
/*  46 */   private static String FN_PURGE = "purge";
/*  47 */   private static String FN_ID = "id";
/*  48 */   private static String FN_NAME = "name";
/*  49 */   private static String FN_LIFETIME = "lifetime";
/*     */   
/*     */   private class SystemPolicy { private SystemPolicy() {}
/*  52 */     Map<String, Policy> keep = Maps.newHashMap();
/*  53 */     Map<String, Policy> purge = Maps.newHashMap();
/*     */     
/*     */      }
/*  56 */   private static RetentionPolicyManager instance = new RetentionPolicyManager();
/*  57 */   private static String SYSTEM_POLICY_KEY = RetentionPolicyManager.class.getSimpleName() + ".SYSTEM_POLICY";
/*     */   
/*     */   public static RetentionPolicyManager getInstance()
/*     */   {
/*  61 */     return instance;
/*     */   }
/*     */   
/*     */   private SystemPolicy getCachedSystemPolicy(Entry entry) throws ServiceException
/*     */   {
/*     */     SystemPolicy sp;
/*  67 */     synchronized (entry) {
/*  68 */       sp = (SystemPolicy)entry.getCachedData(SYSTEM_POLICY_KEY);
/*     */       
/*  70 */       if (sp == null) {
/*     */         String xml;
/*  72 */         if ((entry instanceof Config)) {
/*  73 */           xml = ((Config)entry).getMailPurgeSystemPolicy(); } else { String xml;
/*  74 */           if ((entry instanceof Cos)) {
/*  75 */             xml = ((Cos)entry).getMailPurgeSystemPolicy();
/*     */           } else
/*  77 */             throw ServiceException.UNSUPPORTED(); }
/*     */         String xml;
/*  79 */         sp = new SystemPolicy(null);
/*  80 */         if (!Strings.isNullOrEmpty(xml)) {
/*  81 */           ZimbraLog.purge.debug("Parsing system retention policy:\n%s", new Object[] { xml });
/*     */           try {
/*  83 */             Element el = Element.parseXML(xml);
/*  84 */             RetentionPolicy rp = (RetentionPolicy)JaxbUtil.elementToJaxb(el, RetentionPolicy.class);
/*  85 */             for (Policy p : rp.getKeepPolicy()) {
/*  86 */               assert (p.getId() != null);
/*  87 */               sp.keep.put(p.getId(), p);
/*     */             }
/*  89 */             for (Policy p : rp.getPurgePolicy()) {
/*  90 */               assert (p.getId() != null);
/*  91 */               sp.purge.put(p.getId(), p);
/*     */             }
/*     */           } catch (XmlParseException e) {
/*  94 */             throw ServiceException.FAILURE("Unable to parse system retention policy.", e);
/*     */           }
/*     */         }
/*  97 */         entry.setCachedData(SYSTEM_POLICY_KEY, sp);
/*     */       }
/*     */     }
/* 100 */     return sp;
/*     */   }
/*     */   
/*     */   public Policy createSystemKeepPolicy(Entry entry, String name, String lifetime) throws ServiceException
/*     */   {
/* 105 */     validateLifetime(lifetime);
/* 106 */     Policy p = Policy.newSystemPolicy(generateId(), name, lifetime);
/* 107 */     synchronized (entry) {
/* 108 */       SystemPolicy sp = getCachedSystemPolicy(entry);
/* 109 */       sp.keep.put(p.getId(), p);
/* 110 */       saveSystemPolicy(entry, new RetentionPolicy(sp.keep.values(), sp.purge.values()));
/*     */     }
/* 112 */     return p;
/*     */   }
/*     */   
/*     */   public Policy createSystemPurgePolicy(Entry entry, String name, String lifetime) throws ServiceException
/*     */   {
/* 117 */     validateLifetime(lifetime);
/* 118 */     Policy p = Policy.newSystemPolicy(generateId(), name, lifetime);
/* 119 */     synchronized (entry) {
/* 120 */       SystemPolicy sp = getCachedSystemPolicy(entry);
/* 121 */       sp.purge.put(p.getId(), p);
/* 122 */       saveSystemPolicy(entry, new RetentionPolicy(sp.keep.values(), sp.purge.values()));
/*     */     }
/* 124 */     return p;
/*     */   }
/*     */   
/*     */   private static void validateLifetime(String lifetime) throws ServiceException
/*     */   {
/* 129 */     if (Strings.isNullOrEmpty(lifetime)) {
/* 130 */       throw ServiceException.INVALID_REQUEST("lifetime not specified", null);
/*     */     }
/* 132 */     long l = DateUtil.getTimeInterval(lifetime, -1L);
/* 133 */     if (l == -1L) {
/* 134 */       throw ServiceException.INVALID_REQUEST("Invalid lifetime value: " + lifetime, null);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public Policy modifySystemPolicy(Entry entry, String id, String name, String lifetime)
/*     */     throws ServiceException
/*     */   {
/* 144 */     validateLifetime(lifetime);
/* 145 */     synchronized (entry) {
/* 146 */       SystemPolicy sp = getCachedSystemPolicy(entry);
/* 147 */       if (sp.keep.containsKey(id)) {
/* 148 */         Policy p = Policy.newSystemPolicy(id, name, lifetime);
/* 149 */         sp.keep.put(id, p);
/* 150 */         saveSystemPolicy(entry, new RetentionPolicy(sp.keep.values(), sp.purge.values()));
/* 151 */         return p;
/*     */       }
/*     */       
/* 154 */       if (sp.purge.containsKey(id)) {
/* 155 */         Policy p = Policy.newSystemPolicy(id, name, lifetime);
/* 156 */         sp.purge.put(id, p);
/* 157 */         saveSystemPolicy(entry, new RetentionPolicy(sp.keep.values(), sp.purge.values()));
/* 158 */         return p;
/*     */       }
/*     */     }
/* 161 */     return null;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public boolean deleteSystemPolicy(Entry entry, String id)
/*     */     throws ServiceException
/*     */   {
/* 171 */     synchronized (entry) {
/* 172 */       SystemPolicy sp = getCachedSystemPolicy(entry);
/* 173 */       Policy p = (Policy)sp.keep.remove(id);
/* 174 */       if (p == null) {
/* 175 */         p = (Policy)sp.purge.remove(id);
/*     */       }
/* 177 */       if (p != null) {
/* 178 */         saveSystemPolicy(entry, new RetentionPolicy(sp.keep.values(), sp.purge.values()));
/* 179 */         return true;
/*     */       }
/*     */     }
/* 182 */     return false;
/*     */   }
/*     */   
/*     */   private void saveSystemPolicy(Entry entry, RetentionPolicy rp) throws ServiceException
/*     */   {
/* 187 */     String xml = JaxbUtil.jaxbToElement(rp, Element.XMLElement.mFactory).prettyPrint();
/* 188 */     if ((entry instanceof Config)) {
/* 189 */       ((Config)entry).setMailPurgeSystemPolicy(xml);
/* 190 */     } else if ((entry instanceof Cos))
/* 191 */       ((Cos)entry).setMailPurgeSystemPolicy(xml);
/*     */   }
/*     */   
/*     */   private String generateId() {
/* 195 */     return UUID.randomUUID().toString();
/*     */   }
/*     */   
/*     */   public RetentionPolicy getSystemRetentionPolicy(Entry entry) throws ServiceException
/*     */   {
/* 200 */     SystemPolicy sp = getCachedSystemPolicy(entry);
/* 201 */     return new RetentionPolicy(sp.keep.values(), sp.purge.values());
/*     */   }
/*     */   
/*     */   public RetentionPolicy getSystemRetentionPolicy(Account acct)
/*     */     throws ServiceException
/*     */   {
/* 207 */     RetentionPolicy retentionPolicy = null;
/* 208 */     Cos cos = acct.getCOS();
/* 209 */     if (cos != null)
/* 210 */       retentionPolicy = getInstance().getSystemRetentionPolicy(cos);
/* 211 */     if ((retentionPolicy == null) || (!retentionPolicy.isSet())) {
/* 212 */       Config config = Provisioning.getInstance().getConfig();
/* 213 */       retentionPolicy = getInstance().getSystemRetentionPolicy(config);
/*     */     }
/* 215 */     return retentionPolicy;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private RetentionPolicy getCompleteRetentionPolicy(RetentionPolicy master, RetentionPolicy rp)
/*     */     throws ServiceException
/*     */   {
/* 224 */     return new RetentionPolicy(getLatestList(master, rp.getKeepPolicy()), getLatestList(master, rp.getPurgePolicy()));
/*     */   }
/*     */   
/*     */   public RetentionPolicy getCompleteRetentionPolicy(Account acct, RetentionPolicy rp)
/*     */     throws ServiceException
/*     */   {
/* 230 */     RetentionPolicy retentionPolicy = getInstance().getSystemRetentionPolicy(acct);
/* 231 */     retentionPolicy = getInstance().getCompleteRetentionPolicy(retentionPolicy, rp);
/* 232 */     return retentionPolicy;
/*     */   }
/*     */   
/*     */   private List<Policy> getLatestList(RetentionPolicy master, Iterable<Policy> list) throws ServiceException
/*     */   {
/* 237 */     List<Policy> latestList = Lists.newArrayList();
/* 238 */     for (Policy policy : list) {
/* 239 */       if (policy.getType() == Policy.Type.USER) {
/* 240 */         latestList.add(policy);
/*     */       } else {
/* 242 */         Policy latest = master.getPolicyById(policy.getId());
/* 243 */         if (latest != null) {
/* 244 */           latestList.add(latest);
/*     */         }
/*     */       }
/*     */     }
/* 248 */     return latestList;
/*     */   }
/*     */   
/*     */   public Policy getPolicyById(Entry entry, String id) throws ServiceException
/*     */   {
/* 253 */     SystemPolicy sp = getCachedSystemPolicy(entry);
/* 254 */     Policy p = (Policy)sp.keep.get(id);
/* 255 */     if (p != null) {
/* 256 */       return p;
/*     */     }
/* 258 */     p = (Policy)sp.purge.get(id);
/* 259 */     if (p != null) {
/* 260 */       return p;
/*     */     }
/* 262 */     return null;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static Metadata toMetadata(RetentionPolicy rp, boolean forMailbox)
/*     */   {
/* 274 */     MetadataList keep = new MetadataList();
/* 275 */     MetadataList purge = new MetadataList();
/*     */     
/* 277 */     for (Policy p : rp.getKeepPolicy()) {
/* 278 */       keep.add(toMetadata(p, forMailbox));
/*     */     }
/* 280 */     for (Policy p : rp.getPurgePolicy()) {
/* 281 */       purge.add(toMetadata(p, forMailbox));
/*     */     }
/*     */     
/* 284 */     Metadata m = new Metadata();
/* 285 */     m.put(FN_KEEP, keep);
/* 286 */     m.put(FN_PURGE, purge);
/* 287 */     return m;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static Metadata toMetadata(Policy p, boolean forMailbox)
/*     */   {
/* 299 */     Metadata m = new Metadata();
/* 300 */     if (p.getType() == Policy.Type.USER) {
/* 301 */       m.put(FN_LIFETIME, p.getLifetime());
/*     */     } else {
/* 303 */       m.put(FN_ID, p.getId());
/* 304 */       if (!forMailbox) {
/* 305 */         m.put(FN_NAME, p.getName());
/* 306 */         m.put(FN_LIFETIME, p.getLifetime());
/*     */       }
/*     */     }
/* 309 */     return m;
/*     */   }
/*     */   
/*     */   public static RetentionPolicy retentionPolicyFromMetadata(Metadata m, boolean forMailbox) throws ServiceException
/*     */   {
/* 314 */     if (m == null) {
/* 315 */       return new RetentionPolicy();
/*     */     }
/*     */     
/* 318 */     List<Policy> keep = Collections.emptyList();
/* 319 */     List<Policy> purge = Collections.emptyList();
/*     */     
/* 321 */     MetadataList keepMeta = m.getList(FN_KEEP, true);
/* 322 */     if (keepMeta != null) {
/* 323 */       keep = policyListFromMetadata(keepMeta, forMailbox);
/*     */     }
/* 325 */     MetadataList purgeMeta = m.getList(FN_PURGE, true);
/* 326 */     if (purgeMeta != null) {
/* 327 */       purge = policyListFromMetadata(purgeMeta, forMailbox);
/*     */     }
/*     */     
/* 330 */     return new RetentionPolicy(keep, purge);
/*     */   }
/*     */   
/*     */   private static List<Policy> policyListFromMetadata(MetadataList ml, boolean forMailbox) throws ServiceException
/*     */   {
/* 335 */     List<Policy> policyList = Lists.newArrayList();
/* 336 */     if (ml != null) {
/* 337 */       for (int i = 0; i < ml.size(); i++) {
/* 338 */         Policy policy = policyFromMetadata(ml.getMap(i), forMailbox);
/* 339 */         if (policy != null) {
/* 340 */           policyList.add(policy);
/*     */         }
/*     */       }
/*     */     }
/* 344 */     return policyList;
/*     */   }
/*     */   
/*     */   private static Policy policyFromMetadata(Metadata m, boolean forMailbox) throws ServiceException
/*     */   {
/* 349 */     String id = m.get(FN_ID, null);
/* 350 */     if (id != null) {
/* 351 */       if (forMailbox) {
/* 352 */         return Policy.newSystemPolicy(id);
/*     */       }
/* 354 */       return Policy.newSystemPolicy(id, m.get(FN_NAME), m.get(FN_LIFETIME));
/*     */     }
/*     */     
/* 357 */     return Policy.newUserPolicy(m.get(FN_LIFETIME));
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/RetentionPolicyManager.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */